//
//  ActivitiesDetailViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivitiesDetailViewController.h"
#import "LoginViewController.h"
#import "UIImageView+WebCache.h"
#import "DataBaseSingleng.h"
@interface ActivitiesDetailViewController ()

@end

@implementation ActivitiesDetailViewController

- (void)dealloc
{
    [_actList release];
    [_actDV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    CGFloat scrollHight = 0.0;
    [self.view addSubview:scrollView];
    [scrollView release];
    
    
    self.actDV = [[ActivitiesDetailView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.actDV.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:self.actDV];
    [_actDV release];
    
    
    
    //传值
    
    self.navigationItem.title = self.actDV.title.text;
    self.actDV.title.text = self.actList.title;
    self.actDV.address.text = self.actList.address;
    self.actDV.owner.text = self.actList.name;
    self.actDV.category.text = self.actList.category_name;
    self.actDV.content2.text = self.actList.content;
    [self.actDV.content2 sizeToFit];
    [self.actDV.address sizeToFit];
   
    
    //取图片
    NSURL *url = [NSURL URLWithString:self.actList.image_hlarge];
    [self.actDV.activitylcon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed: @"Monkey.png"]];
    
    //传时间
    NSString *str1 = [self.actList.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str2 = [self.actList.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str = [NSString stringWithFormat:@"%@ -- %@",str1,str2];
    self.actDV.timeLabel.text = str;
   
   /*
    //scrollView高度自适应 (不理想)
    for (UIView *view in self.actDV.subviews) {
        scrollHight += view.frame.size.height;
    }
    [scrollView setContentSize:CGSizeMake(375, scrollHight)];
    */
    
    //scrollView高度自适应
    scrollHight = self.actDV.content2.frame.origin.y + self.actDV.content2.frame.size.height + 60;
    [scrollView setContentSize:CGSizeMake(375,scrollHight)];
    
    
    
    //设置背景
    self.actDV.backgroundImageView.image = [UIImage imageNamed:@"aaa.jpg"];
    //背景自适应
    self.actDV.backgroundImageView.frame = CGRectMake(0, 0, 375, scrollHight);
    //透明度
    self.actDV.backgroundImageView.alpha = 0.3;
    
    
    
    
    //设置左边按钮
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    
    //取消渲染
    leftImage  = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationItem.leftBarButtonItem = [UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(liftButton:)];
    
    
    
    //设置右边按钮
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    //取消渲染
    rightImage = [rightImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(rightButton:)];
    
    
}

#pragma mark -- 右边按钮方法
-(void)rightButton:(UIButton *)button
{
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    
    if (![@"1" isEqualToString:isLogin]) {
        
        // 如果未登录
        LoginViewController *logVC = [[LoginViewController alloc] init];
        
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:logVC];
        
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
        
        [logVC release];
        [naVC release];
    }else{
        
       
        
       NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

         NSMutableArray *array = [[DataBaseSingleng shareDataBase]selectMyActvity:name];

        BOOL result = [array containsObject:self.actDV.title.text];
        if (result) {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"此活动已收藏过" preferredStyle:UIAlertControllerStyleAlert];
            
                            [self presentViewController:alertC animated:YES completion:^{
            
                                [self dismissViewControllerAnimated:YES completion:^{
            
            
                                }];
                                
                            }];

        }else{
            
             [[DataBaseSingleng shareDataBase]insertMyActivity:self.actDV.title.text :name];
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alertC animated:YES completion:^{
                
                [self dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            }];

            
        }
    }
}

//左边按钮
-(void)liftButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
